package org.luxor.schedule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.annotation.SysLog;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.commons.schedule.entity.ScheduleJobEntity;
import org.luxor.schedule.service.impl.ScheduleJobServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 定时任务
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.2.0 2016-11-28
 */
@Api(tags = "Schedule定时任务")
@RestController
@RequestMapping("/sys/schedule/job")
public class ScheduleJobController extends BaseRestController {

    @Resource
    private ScheduleJobServiceImpl scheduleJobService;

    /**
     * 定时任务列表
     */
    @PostMapping("/list")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:list')")
    @ApiOperation("定时任务列表")
    public R<IPage<ScheduleJobEntity>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<ScheduleJobEntity> data = scheduleJobService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{jobId}")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:info')")
    @ApiOperation("定时任务信息")
    public R<ScheduleJobEntity> info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.getById(jobId);
        return R.ok(schedule);
    }

    /**
     * 保存定时任务
     */
    @SysLog("保存定时任务")
    @PostMapping("/save")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:save')")
    @ApiOperation("保存定时任务")
    public R save(@Validated(AddGroup.class) @RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.saveJob(scheduleJob);
        return R.ok();
    }

    /**
     * 修改定时任务
     */
    @SysLog("修改定时任务")
    @PutMapping("/update")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:update')")
    @ApiOperation("修改定时任务")
    public R update(@Validated(UpdateGroup.class) @RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.update(scheduleJob);
        return R.ok();
    }

    /**
     * 删除定时任务
     */
    @SysLog("删除定时任务")
    @DeleteMapping("/delete")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:delete')")
    @ApiOperation("删除定时任务")
    public R delete(@RequestBody List<Long> jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return R.ok();
    }

    /**
     * 立即执行任务
     */
    @SysLog("立即执行任务")
    @PostMapping("/run")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:run')")
    @ApiOperation("立即执行任务")
    public R run(@RequestBody List<Long> jobIds) {
        scheduleJobService.run(jobIds);
        return R.ok();
    }

    /**
     * 暂停定时任务
     */
    @SysLog("暂停定时任务")
    @PostMapping("/pause")
    @PreAuthorize("hasPermission(#request,'sys:schedule:job:pause')")
    @ApiOperation("暂停定时任务")
    public R pause(@RequestBody List<Long> jobIds) {
        scheduleJobService.pause(jobIds);
        return R.ok();
    }

    /**
     * 恢复定时任务
     */
    @SysLog("恢复定时任务")
    @PostMapping("/resume")
    @PreAuthorize("hasPermission(#request,'sys:schedule:resume')")
    @ApiOperation("恢复定时任务")
    public R resume(@RequestBody List<Long> jobIds) {
        scheduleJobService.resume(jobIds);
        return R.ok();
    }

}
